<?php
/**
 * ------------------------------------------------------------------------
 * JA Toppanel Module for Joomla 2.5
 * ------------------------------------------------------------------------
 * Copyright (C) 2004-2011 J.O.O.M Solutions Co., Ltd. All Rights Reserved.
 * @license - GNU/GPL, http://www.gnu.org/licenses/gpl.html
 * Author: J.O.O.M Solutions Co., Ltd
 * Websites: http://www.joomlart.com - http://www.joomlancers.com
 * ------------------------------------------------------------------------
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

require_once JPATH_SITE . '/components/com_content/helpers/route.php';
JModel::addIncludePath(JPATH_SITE . '/components/com_content/models');
/**
 *
 * JA Top Panel Helper class
 * @author JoomlArt
 *
 */
class modJaToppanelHelper
{
    var $_result = null;
    var $_params = null;


    /**
     *
     * Load data
     * @param object $params
     * @return array data
     */
    function _load($params)
    {
        $this->_params = $params;
        switch ($params->get('type')) {
            case 'modules':
                $this->rend_html_Modules();
                break;
            case 'module':
                $this->rend_html_Module();
                break;
            case 'article-ids':
            case 'article-catid':
                $this->rend_html_Article();
                break;
        }

        return $this->_result;
    }


    /**
     *
     * Load modules data
     * @return array data
     */
    function rend_html_Modules()
    {
        $lis = '';
        $list = JModuleHelper::getModules(trim($this->_params->get('modules', '')));
        $mparams = array('style' => 'jaxhtml');
        $countlist = count($list) - 1;
        for ($j = 0; $j <= $countlist; $j++) {
            if ($j == 0) {
                $itemclass = 'toppanel-item first-item';
            } elseif ($j == $countlist) {
                $itemclass = 'toppanel-item last-item';
            } else {
                $itemclass = 'toppanel-item';
            }
            if ($list[$j]->module != 'mod_jatoppanel') {
                $lis .= "<li class=\"" . $itemclass . "\" title=\"" . strip_tags($list[$j]->title) . "\">";
                $lis .= JModuleHelper::renderModule($list[$j], $mparams);
                $lis .= '</li>';
            }
        }

        if ($lis != '') {
            $class = $this->_params->get('display', 'ja-toppanel-col');
            $this->writeHTML($lis, $class);
        }
    }


    /**
     *
     * Load module data
     * @return array data
     */
    function rend_html_Module()
    {
        $lis = '';
        $list_module = array();
        if ($this->_params->get('modulename', '')){
            $list_module = explode(",", $this->_params->get('modulename'));
        }
        $countlist = count($list_module) - 1 ;
        if($countlist+1){
            for ($i = 0; $i < count($list_module); $i++) {
                if ($list_module[$i] != 'mod_jatoppanel') {
                    $module = JModuleHelper::getModule(substr(trim($list_module[$i]), 4));
                    if ($module && $module->id) {
                        if ($i == 0) {
                            $itemclass = 'toppanel-item first-item';
                        } elseif ($i == $countlist) {
                            $itemclass = 'toppanel-item last-item';
                        } else {
                            $itemclass = 'toppanel-item';
                        }
                        $lis .= "<li class=\"" . $itemclass . "\" title=\"" . strip_tags($module->title) . "\"><h3><span>" . $module->title . "</span></h3>";
                        $lis .= JModuleHelper::renderModule($module);
                        $lis .= '</li>';
                    }
                }

            }
        }
        if ($lis != '') {
            $class = $this->_params->get('display', 'ja-toppanel-col');
            $this->writeHTML($lis, $class);
        }
    }


    /**
     *
     * Load articles data
     * @return array data
     */
    function rend_html_Article()
    {
        $list = null;
        $lis = '';
        if ($this->_params->get('type') == 'article-ids' && $this->_params->get('article-ids', '')) {
            $list = $this->getList($this->_params->get('article-ids', ''));
        } elseif ($this->_params->get('type') == 'article-catid') {
            $catids = (is_array($this->_params->get('article-catid', array())) && $this->_params->get('article-catid', array())) ? implode(',', $this->_params->get('article-catid', '')) : $this->_params->get('article-catid', '');
            $list = $this->getList('', $catids, $this->_params->get('limit', -1));
        }

        if ($list) {
            $introtext_limit = $this->_params->get('maxchars', 100);
            $countlist = count($list) - 1 ;
            $i = 0 ;
            foreach ($list as $row) {
                if ($i == 0) {
                    $itemclass = 'toppanel-item first-item';
                } elseif ($i == $countlist) {
                    $itemclass = 'toppanel-item last-item';
                } else {
                    $itemclass = 'toppanel-item';
                }
				
				$title_char = strlen($row->introtext)>$introtext_limit?substr($row->introtext, 0, $introtext_limit).'...':$row->introtext;
				$title_char = strip_tags($title_char,"<p><a><div><table><tr><td><img>");
                $row->displayIntrotext = JHtml::_('string.truncate', JHtml::_('content.prepare', $title_char), '<p><a>', $introtext_limit);

                $lis .= "<li class=\"" . $itemclass . "\" title=\"" . strip_tags($row->title) . "\"><div class='ja-toppanel-article'><h3><span><a href='$row->link' title='{$row->title}'>{$row->title}</a></span></h3>";
                $lis .= $row->displayIntrotext;
                $lis .= '</div></li>';
                $i++ ;
            }

            $class = $this->_params->get('display', 'ja-toppanel-col');
            $this->writeHTML($lis, $class);
        }

        return '';
    }


    /**
     *
     * Display content
     * @param string $content content data
     * @param string $class class for wrapper
     * @return void
     */
    function writeHTML($content, $class = '')
    {
        $this->_result = "<ul class='$class'>";
        $this->_result .= $content;
        $this->_result .= '</ul>';

    }


    /**
     *
     * Get articles
     * @param string $ids
     * @param string $catid
     * @param int $limit
     * @return array
     */
    public function getList($ids = '', $catid = '', $limit = 0)
    {
        // Get the dbo
        $db = JFactory::getDbo();

        // Get an instance of the generic articles model
        $model = JModel::getInstance('Articles', 'ContentModel', array('ignore_request' => true));

        // Set application parameters in model
        $appParams = JFactory::getApplication()->getParams();
        $model->setState('params', $appParams);

        // Set the filters based on the module params
        $model->setState('list.start', 0);
        if (($catid != '' || $this->_params->get('type') == 'article-catid') && $limit > 0) {
            $model->setState('list.limit', $limit);
        }

        $model->setState('filter.published', 1);

        // Access filter
        $access = !JComponentHelper::getParams('com_content')->get('show_noauth');
        $authorised = JAccess::getAuthorisedViewLevels(JFactory::getUser()->get('id'));
        $model->setState('filter.access', $access);

        // Category filter
        if ($catid) {
            $model->setState('filter.category_id', explode(',', $catid));
        }
        if ($ids) {
            $model->setState('filter.article_id', explode(',', $ids));
        }

        // Set ordering
        $order_map = array('m_dsc' => 'a.modified DESC, a.created', 'mc_dsc' => 'CASE WHEN (a.modified = ' . $db->quote($db->getNullDate()) . ') THEN a.created ELSE a.modified END', 'c_dsc' => 'a.created', 'p_dsc' => 'a.publish_up');
        $ordering = JArrayHelper::getValue($order_map, 'a.ordering', 'a.publish_up');
        $dir = 'DESC';

        $model->setState('list.ordering', $ordering);
        $model->setState('list.direction', $dir);

        $items = $model->getItems();

        foreach ($items as &$item) {
            $item->slug = $item->id . ':' . $item->alias;
            $item->catslug = $item->catid . ':' . $item->category_alias;

            if ($access || in_array($item->access, $authorised)) {
                // We know that user has the privilege to view the article
                $item->link = JRoute::_(ContentHelperRoute::getArticleRoute($item->slug, $item->catslug));
            } else {
                $item->link = JRoute::_('index.php?option=com_user&view=login');
            }

            $item->introtext = JHtml::_('content.prepare', $item->introtext);
        }

        return $items;
    }


    /**
     *
     * Detect IE browser
     */
    function ae_detect_ie()
    {
        if (isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false))
            return true;
        else
            return false;
    }
}

?>
